<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22 0022
 * Time: 上午 10:18
 */

namespace App\Http\Controllers\Xcx;


use App\Craftsman;
use App\Exceptions\CreateException;
use App\Extend\Tools;
use App\Http\Controllers\Controller;
use App\Item;
use App\Area;
use App\Member;
use App\Team;
use App\Project;
use App\Needwork;
use Carbon\Carbon;
use App\Xm_history;
use App\Gc_history;
use App\Xm_register;
use Illuminate\Http\Request;

/**
 * @property Craftsman model
 */
class WorkController extends Controller
{
    public function __construct(Craftsman $model)
    {
        $this->model = $model;
    }
    
    public function works(Request $request)
    {
        $size = $request -> input('size',10);
        $page = $request -> input('page',1);
        $province = $request -> input('province','');
        $city = $request -> input('city','');
        $addr = $request -> input('addr','');
        $data = Needwork::where('shenhe','=','4')->where('province','=',$province)->where('city','=',$city)->where('addr','=',$addr)->limit($size)->offset(($page-1)*$size)->get();
        //如果有搜索的内容则重新查找
        if($province=='' && $city=='' && $addr=='') {
            $data = Needwork::where('shenhe','=','4')->limit($size)->offset(($page-1)*$size)->get();
        }
        $arr = [];
        foreach ($data as $k=>$v) {
            $city = Area::find($v->city);
            $addr = Area::find($v->addr);
            $work = Team::find($v->work[0]);
            $arr[$k]['title'] = $city['name'].$addr['name'].'招'.$work['name'];
            $arr[$k]['money'] = $v->money;
            $arr[$k]['men'] = $v->sum;
            $arr[$k]['time'] = $v->times;
            $arr[$k]['address'] = $city['name'].$addr['name'];
            $arr[$k]['id'] = $v->id;
        }
        // ->limit($size)->offset(($page-1)*$size)->get();
        return $this->success($arr);
    }

    public function workinfo(Request $request,$id)
    {
        $warning = "这是一段警告\r\n这是一段警告\r\n这是一段警告\r\n这是一段警告";
            $data = Needwork::find($id);
            $s = Team::find($data->work[0]);
            $arr = [];
            $city = Area::find($data->city);
            $addr = Area::find($data->addr);
            $address = $city.$addr;
            $arr['title'] = $data->address.'招';
            if($s != null) {
                $arr['title'] = $data->address.'招'.$s->name;
            }
            $arr['money'] = $data->money;
            $arr['men'] = $data->sum;
            $arr['warning'] = $warning;
            $arr['time'] = $data->times;
            $arr['desc'] = $data->desc;
            $arr['address'] = $data->addr;
            $arr['id'] = $data->id;
            $arr['user'] = Xm_register::where('uid',$data['uid'])->first()['x_name'];
            $arr['phone'] = Tools::getMaskPhone($data['phone']);
                $n = Xm_history::where('g_id',$id)->where('id',auth('xcx')->user()->id)->first();
                if($n == null) {
                        $xm_history = new Xm_history;
                        $xm_history -> g_id = $id;
                        $xm_history -> id = auth('xcx')->user()->id;
                        $xm_history -> save();
                }
        return $this->success($arr);
    }

    public function liulan(Request $request) //浏览的班组记录
    {
        $page  = $request->input('page',1);
        $size  = $request->input('size',10);
        $res = Xm_history::where('id',auth('xcx')->user()->id)->limit($size)->offset(($page-1)*$size)->get(); //查找当前你浏览过的历史记录并分页
        $a = []; //定义一个空数组
        foreach ($res as $v) {
            $a[] = $v->g_id; //将浏览过的班组id存入数组中
        }
        // return $this->success($a);
        $s = Needwork::find($a);//查找项目招工表中的浏览过的数据
        $data = []; 
        foreach ($s as $k=>$v) {
            $a = Team::find($v->work[0]);
            $city = Area::find($v->city);
            $addr = Area::find($v->addr);
            $address = $city.$addr;
            $data[$k]['title'] = $v->address.'招';//拼接的地址
            if($a != null) {
                $data[$k]['title'] = $v->address.'招'.$a->name;
            }
            $data[$k]['money'] = $v->money; //薪资
            $data[$k]['men'] = $v->sum; //人数
            $data[$k]['address'] = $v->addr; //城市
            $data[$k]['time'] = $v->times; //项目开始时间
            $data[$k]['id'] =  $v->id;
        }
        return $this->success($data);
       
    }
    
}